<template>
  <div class="plan-container">
    <div class="plan-search">
      <el-form :inline="true" :model="searchform" ref="searchform" class="demo-form-inline">
        <el-form-item label="任务名称" prop="key">
          <el-input v-model="searchform.key" placeholder="任务名称" size="small"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="listPage">查询</el-button>
          <el-button type="primary" size="mini" @click="resetForm('searchform')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="plan-body">
      <el-button type="primary" size="mini" style="margin-bottom: 20px;" @click="addjob">添加任务</el-button>
      <el-table :data="plans" style="width: 100%;margin-bottom: 20px;" row-key="id" border>
        <el-table-column prop="name" label="计划名称"></el-table-column>
        <el-table-column prop="categroy" label="计划分组"></el-table-column>
        <el-table-column prop="jobkey" label="关联任务"></el-table-column>
        <el-table-column prop="status" label="计划状态">
          <template v-slot="{row}">
            <el-switch
              v-model="row.status"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleStatus(row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="descript" label="计划描述"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchform.pageNum"
        :page-sizes="[5, 10, 15]"
        :page-size="searchform.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 新增 / 编辑 -->
    <add-and-update ref="addAndUpdate" v-model="dialogVisible" @refresh="refresh"></add-and-update>
  </div>
</template>

<script>
import {
  listPlanPage,
  delPlan,
  startPlan,
  updatePlan,
  pausePlan
} from "@/api/task";
import addAndUpdate from "./add-and-update";
import { Row } from "element-ui";
export default {
  name: "job",
  components: { addAndUpdate },
  data() {
    return {
      searchform: {
        pageNum: 1,
        pageSize: 5,
        key: ""
      },
      total: 0,
      plans: [],
      jobId: null,
      dialogVisible: false
    };
  },
  created() {
    this.listPage();
  },
  methods: {
    async listPage() {
      const { data } = await listPlanPage(this.searchform);
      this.plans = data.data;
      this.searchform.pageNum = data.pageNum;
      this.searchform.pageSize = data.pageSize;
      this.total = data.total;
    },
    addjob() {
      this.dialogVisible = true;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.searchform.pageNum = val;
      this.listPage(this.searchform);
    },
    handleEdit(index, row) {
      this.$refs["addAndUpdate"].init({ ...row });
      this.$nextTick(() => {
        this.dialogVisible = true;
      });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await delPlan(row.id, row.categroy);
          if (index === 0) {
            this.searchform.pageNum--;
          }
          this.listPage();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async handleStatus(row) {
      if (row.status === 1) {
        await startPlan(row.id, row.categroy);
      } else {
        await pausePlan(row.id, row.categroy);
      }
      this.listPage()
    },
    resetForm(formname) {
      this.$refs[formname].resetFields();
    },
    refresh() {
      this.dialogVisible = false;
      this.listPage();
    }
  }
};
</script>

<style lang="scss" scoped>
.plan {
  &-container {
    margin: 30px;
  }

  &-search {
    height: 70px;
    padding: 10px 10px;
    border-radius: 5px;
    background-color: #fff;
  }

  &-body {
    border-radius: 5px;
    background-color: #fff;
    margin: 20px 0px;
    padding: 20px 10px;
  }
}
.el-pagination {
  text-align: center;
}
</style>